package com.sockect3d.data;

import android.annotation.SuppressLint;

import java.math.BigInteger;
import java.util.Arrays;

/**
 * Created by Jaqen on 2015/11/24
 * DataUtils
 */
@SuppressLint("NewApi") public class DataUtils {
    /**
     *
     * @param value
     * @return WORD类型的字节
     */
    public static byte[] getWordBytes(int value){
        return numberToBytes(value, 2);
    }

    /**
     *
     * @param value
     * @return DWORD类型的字节
     */
    public static byte[] getDwordBytes(long value) {

        return numberToBytes(value, 4);
    }

    /**
     *
     * @param value
     * @return long类型的字节
     */
    public static byte[] longToBytes(long value){

        return numberToBytes(value, 8);
    }

    /*public static byte[] getULongBytes(String ulongStr){
        BigInteger bigInteger = new BigInteger(ulongStr);
        byte[] ulongByteHH = bigInteger.toByteArray();
        byte[] ulongByteLH = new byte[8];

        int minLength = Math.min(ulongByteHH.length, 8);

        for (int i = 0; i < minLength; i ++){

        }
    }*/

    /**
     *
     * @param value
     * @return int的字节
     */
    public static byte[] intToBytes(int value){
        return numberToBytes(value, 4);
    }

    public static int bytesToInt(byte[] valueBytes){
        byte[] intByte = new byte[4];

        intByte[3] = valueBytes[0];
        intByte[2] = valueBytes[1];
        intByte[1] = valueBytes[2];
        intByte[0] = valueBytes[3];

        BigInteger bigInteger = new BigInteger(intByte);

        return  bigInteger.intValue();
//    	int iOutcome = 0;
//        byte bLoop;
//
//        for (int i = 0; i < valueBytes.length; i++) {
//            bLoop = valueBytes[i];
//            iOutcome += (bLoop & 0xFF) << (8 * i);
//        }
//        return iOutcome;
    }

    public static long byteToLong(byte[] valueBytes){
        byte[] longByte = new byte[8];

        longByte[7] = valueBytes[0];
        longByte[6] = valueBytes[1];
        longByte[5] = valueBytes[2];
        longByte[4] = valueBytes[3];
        longByte[3] = valueBytes[4];
        longByte[2] = valueBytes[5];
        longByte[1] = valueBytes[6];
        longByte[0] = valueBytes[7];

        BigInteger bigInteger = new BigInteger(longByte);

        return bigInteger.longValue();
    }

    public static long dwordToLong(byte[] dword){
        int firstByte = (0x000000FF & ((int) dword[0]));
        int secondByte = (0x000000FF & ((int) dword[1]));
        int thirdByte = (0x000000FF & ((int) dword[2]));
        int fourthByte = (0x000000FF & ((int) dword[3]));

        return ((long) (firstByte | secondByte << 8 | thirdByte << 16 | fourthByte << 24)) & 0xFFFFFFFFL;
    }

    public static int wordToInt(byte[] word){
        //short firstByte = 0x00FF & ((short)word[0]);
        byte[] intByte = new byte[4];

        intByte[3] = word[0];
        intByte[2] = word[1];

        BigInteger bigInteger = new BigInteger(intByte);

        return  bigInteger.intValue();
    }

    public static byte[] numberToBytes(long value, int byteLength){
        byte[] data = new byte[byteLength];

        for (int i = 0; i < byteLength; i ++){
            data[i] = (byte) (value >> (8 * i) & 0xff);
        }

        return data;
    }

    public static byte[] numberToBytes(int value, int byteLength){
        byte[] data = new byte[byteLength];

        for (int i = 0; i < byteLength; i ++){
            data[i] = (byte) (value >> (8 * i) & 0xff);
        }

        return data;
    }

    public static byte[] combineBytesData(byte[] data1, byte[] data2){
    	if(data2==null){
    		byte[] data = Arrays.copyOf(data1, data1.length);
    		return data;
    	}else{
    		byte[] data = Arrays.copyOf(data1, data1.length + data2.length);
            System.arraycopy(data2, 0, data, data1.length, data2.length);
            return data;
    	}
    	
        

        
    }
}
